<extend name="Layout/ins_page" />
<block name="content">
	<input type="hidden" name="ajax" id="ajax" value="1">
	<select name="folder_list" id="folder_list" class="hidden">
		<option value="0">根节点</option>
		{:fill_option($folder_list)}
	</select>
	{:W('PageHeader/simple',array('name'=>$folder_name))}
	<div class="oprator-panel">
		<div class="split"></div>
		<a id="btn_add" class="btn">新增</a>
		<a id="btn_save" class="btn">保存</a>
		<span class="l">|</span>
		<a id="btn_del" class="btn btn-del">删除</a>
	</div>
	<div class="sub-frame">
		<div class="sub-sidebar gray ">
			<?php echo $menu ?>
		</div>
		<div class="sub-content gray">
			<form id="form_data" name="form_data" method="post" >
				<input type="hidden" name="id" id="id" >
				<input type="hidden" name="opmode" id="opmode" value="">
				<input type="hidden" name="admin" id="admin">
				<input type="hidden" name="write" id="write">
				<input type="hidden" name="read" id="read">
				<div class="layui-sub-form-item">
					<label class="layui-form-label" for="name">名称*：</label>
					<div class="layui-input-block">
						<input class="layui-input" type="text" id="name" name="name" check="required" info="请输入名称">
					</div>
				</div>
				<if condition="$has_pid">
					<div class="layui-sub-form-item">
						<label class="layui-form-label" for="folder_name">父节点*：</label>
						<div class="layui-input-block">
							<div class="input-group">
								<input type="hidden" name="pid" id="pid">
								<input name="folder_name" class="layui-input val" id="folder_name" type="text" info="请选择父节点" check="required" />
								<a class="add-on" id="btn_select_pid" type="button"> 选择 </a>
							</div>
						</div>
					</div>
				</if>
				<div class="layui-sub-form-item">
					<label class="layui-form-label" for="folder_name">管理：</label>
					<div class="layui-input-block">
						<div id="admin_list" class="input-box">
							<a class="add-on select_auth"> <i class="fa fa-user"></i> </a>
							<div class="wrap" >
								<span class="address_list"></span>
								<span class="text" >
									<input class="letter" type="text"  >
								</span>
							</div>
							<div class="search dropdown ">
								<ul class="dropdown-menu"></ul>
							</div>
						</div>
					</div>
				</div>
				<div class="layui-sub-form-item">
					<label class="layui-form-label" for="folder_name">编辑：</label>
					<div class="layui-input-block">
						<div id="write_list" class="input-box">
							<a class="add-on select_auth" > <i class="fa fa-user"></i> </a>
							<div class="wrap" >
								<span class="address_list"></span>
								<span class="text" >
									<input class="letter" type="text"  >
								</span>
							</div>
							<div class="search dropdown ">
								<ul class="dropdown-menu"></ul>
							</div>
						</div>
					</div>
				</div>
				<div class="layui-sub-form-item">
					<label class="layui-form-label" for="folder_name">访问：</label>
					<div class="layui-input-block">
						<div id="read_list" class="input-box">
							<a class="add-on select_auth" > <i class="fa fa-user"></i> </a>
							<div class="wrap" >
								<span class="address_list"></span>
								<span class="text" >
									<input class="letter" type="text"  >
								</span>
							</div>
							<div class="search dropdown ">
								<ul class="dropdown-menu"></ul>
							</div>
						</div>
					</div>
				</div>

				<div class="layui-sub-form-item">
					<label class="layui-form-label" for="sort">排序：</label>
					<div class="layui-input-block">
						<input class="layui-input" type="text" id="sort" name="sort" >
					</div>
				</div>

				<div class="layui-sub-form-item">
					<label class="layui-form-label" for="sort">状态：</label>
					<div class="layui-input-block x-select col-10">
						<select class="layui-input"  name="is_del" id="is_del">
							<option  value="0">启用</option>
							<option value="1">禁用</option>
						</select>
					</div>
				</div>

				<div class="layui-sub-form-item">
					<label class="layui-form-label" for="remark" >备注：</label>
					<div class="layui-input-block" >
						<textarea class="layui-textarea" name="remark" rows="5" class="" ></textarea>
					</div>
				</div>
			</form>
		</div>
	</div>
</block>
<block name="js">
	<script type="text/javascript">
		layui.use('global', function() {
			var $ = layui.jquery;

			$(".sub-sidebar .tree_menu  a").click(function() {
				$(".sub-sidebar .tree_menu  a").attr("class", "");
				$(this).attr("class", "active");
				send_ajax("{:U('SystemFolder/read')}", "id=" + $(this).attr("node"), function(data) {
					showdata(data);
				});
				return false;
			});

			function showdata(result) {
				for (var s in result.data) {
					set_val(s, result.data[s]);
				}
				$("#admin_list span.address_list").html(contact_conv($("#admin").val()));
				$("#write_list span.address_list").html(contact_conv($("#write").val()));
				$("#read_list span.address_list").html(contact_conv($("#read").val()));
				$("#folder_name").val($("#folder_list option[value='" + $("#pid").val() + "']").text());
				$("#opmode").val("edit");
			};

			$('.sub-sidebar .tree_menu a').on('click', function() {
				$this = $(this);
				$(".sub-sidebar .tree_menu a.active").removeClass("active");
				$this.addClass("active");

				node = $this.attr('node');
				$.getJSON("{:U('read')}", 'id=' + node, function(ret) {
					console.log(ret);
					showdata(ret);
				});
				return false;
			});

			$("#btn_add").on('click', function() {
				winopen("{:U('SystemFolder/add',array('controller'=>$controller,'has_pid'=>$has_pid))}", 560, 470);
			});

			$("#btn_save").on('click', function() {
				if ($("#opmode").val() == "") {
					layer.msg("请选择要保存的数据");
					return false;
				}
				$("#admin").val("");
				$("#admin_list span.address_list span").each(function() {
					$("#admin").val($("#admin").val() + $(this).text().replace(';', '') + '|' + $(this).attr("data") + ";");
				});

				$("#write").val("");
				$("#write_list span.address_list span").each(function() {
					$("#write").val($("#write").val() + $(this).text().replace(';', '') + '|' + $(this).attr("data") + ";");
				});

				$("#read").val("");
				$("#read_list span.address_list span").each(function() {
					$("#read").val($("#read").val() + $(this).text().replace(';', '') + '|' + $(this).attr("data") + ";");
				});

				send_form("form_data", "__SELF__", "__SELF__", function(ret) {
					if (ret.status) {
						layer.msg(ret.info);
						location.reload(true);
					}
				});
			});

			$('#select_pid').on('click', function() {
				winopen("{:U('SystemFolder/winpop','controller='.CONTROLLER_NAME)}", 560, 470);
			});

			$('.select_auth').on('click', function() {
				winopen("{:U('popup/auth')}", 560, 470);
			});

			$('#btn_del').on('click', function() {
				if ($(".sub-sidebar a.active").length == 0) {
					layer.msg('请选择要删除的数据');
					return false;
				}
				if ($(".sub-sidebar a.active").parent().find(".tree_menu").html() != undefined) {
					layer.msg('有子目录不能删除');
					return false;
				}
				//信息框-例2
				layer.msg('确定要删除吗?', {
					time : 0,
					btn : ['确定', '取消'],
					yes : function(index) {
						$("#opmode").val("del");
						//send_form("form_data", "", "__SELF__");
						var vars = $("#form_data").serialize();
						send_ajax("__SELF__", vars, function(ret) {
							if (ret.status) {
								layer.msg(ret.info, function() {
									location.reload(true);
								});
							}
						});
					},
					no : function(index) {
						layer.close(index);
					}
				});
			});

			$('.input-box .address_list a.del').on("click", function() {
				$(this).parent().parent().remove();
			});

			set_return_url(null);
		});

	</script>
</block>